/**
 * $version:  0.1 
 * $Date: 2012-04-21 
 *
 * Copyright (C) 2010-2012 Jawa Software. All rights reserved.
 *
 */

package org.jawa.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.jawa.menu.Menu;
import org.jawa.menu.MenuManager;
import org.jawa.util.NotFoundException;
import org.jawa.util.StringUtils;

/**
 * 
 * 项级菜单显示标签.
 * 
 */
public class NavTabsTag extends BodyTagSupport {

	private String role;
	private Long parentMenu;
	private String headercss;
	private String css;
	private String currentcss;

	@Override
	public int doStartTag() throws JspException {
		return EVAL_BODY_BUFFERED;
	}

	@Override
	public int doEndTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

		Long pageID = (Long) request.getAttribute("pageID");

		List<Menu> tabs = MenuManager.getInstance().getTopMenus();

		//当前菜单页
		Menu currentMenu = null;
		//当前菜单上级菜单页
		Menu parrentMenu = null;
		try {
			currentMenu = MenuManager.getInstance().getMenu(pageID);
			parrentMenu = MenuManager.getInstance().getMenu(currentMenu.getMenuParentId());
		} catch (NotFoundException e) {
			//忽略
		}


		if (tabs.size() > 0) {

			JspWriter out = pageContext.getOut();
			StringBuilder buf = new StringBuilder();

			buf.append("<ul ");

			if (null != getHeadercss()) {
				buf.append(" class=\"").append(getHeadercss() + "\" ");
			}
			buf.append(">");

			String body = getBodyContent().getString();

			for (int i = 0; i < tabs.size(); i++) {
				Menu menu = (Menu) tabs.get(i);
				String value = body;
				if (value != null) {
					value = StringUtils.replace(value, "[id]", Long.toString(menu.getMenuId()));

					if (menu.getMenuUrl() != null && !menu.getMenuUrl().trim().equals("#")) {
						value = StringUtils.replace(value, "[url]", request.getContextPath() + "/" + menu.getMenuUrl());
					} else {
						value = StringUtils.replace(value, "[url]", "#");
					}

					value = StringUtils.replace(value, "[name]", menu.getMenuName());

					value = StringUtils.replace(value, "[description]", menu.getRemark());
				}

				String css = getCss();

				if (null != parrentMenu && null != parrentMenu.getParent()
						&& parrentMenu.getParent().getMenuId().equals(menu.getMenuId())) {
					css = getCurrentcss();
				}

				buf.append("<li");
				if (null != css) {
					buf.append(" class=\"").append(css).append("\" ");
				}

				buf.append(">" + value + "</li>");

			}

			buf.append("</ul>");

			try {
				out.write(buf.toString());
			} catch (IOException ioe) {
				throw new JspException(ioe.getMessage());
			}
		}
		return EVAL_PAGE;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public String getCurrentcss() {
		return currentcss;
	}

	public void setCurrentcss(String currentcss) {
		this.currentcss = currentcss;
	}

	public void setHeadercss(String headercss) {
		this.headercss = headercss;
	}

	public String getHeadercss() {
		return headercss;
	}

}